/*=============================================================================
#    The element-comm is a tentative program used to test our ideas.           
#                       Copyright (C)  bgao                                    
#
#     This program is free software; you can redistribute it and/or            
#      modify it under the terms of the GNU General Public License             
#     as published by the Free Software Foundation; either version 2           
#         of the License, or (at your option) any later version.               
#
#     This program is distributed in the hope that it will be useful,          
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            
#             GNU General Public License for more details.                    
#
#    You should have received a copy of the GNU General Public License        
#     along with this program; if not, write to the Free Software             
#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,             
#                       MA  02110-1301, USA.                                  
#==============================================================================
# $Id: mac_info.h 348 2010-11-29 09:01:16Z bgao $ 
#
# $Author: bgao $ 
#
# $Revision: 348 $ 
#
# $Date: 2010-11-29 17:01:16 +0800 (星期一, 29 十一月 2010) $ 
#
# Description: 
#
=============================================================================*/
/*! @file mac_info.h
 *  @brief This file is the interfaces for mac_info module.
 */
#ifndef _MAC_CTRL_H_
#define _MAC_CTRL_H_

#include "common.h"
#include "buf.h"
#include "timer.h"

#define USING_EXTENDED_ADDR 0xfffe
#define BROADCAST_PAN_ID 0xffff
#define BROADCAST_ADDR   0xffff

/*! the MAC layer constants */
typedef enum mac_constant_t_ {
    aBaseSlotDuration           = 60,
    aBaseSuperframeDuration     = 960,
    /* aExtendedAddress, defined above */
    aGTSDescPersistenceTime     = 4,
    aMaxBeaconOverhead          = 75,
    aMaxBeaconPlayloadLength    = 52,
    aMaxLostBeacons             = 4,
    aMaxMACSafePayloadSize      = 102,
    aMaxMACPlayloadSize         = 118,
    aMaxMPDUUnsecuredOverhead   = 25,
    aMaxSIFSFrameSize           = 18,
    aMinCAPLength               = 440,
    aMinMPDUOverhead            = 9,
    aNumSuperframeSlots         = 16,
    aUnitBackoffPeriod          = 20
} mac_constant_t;

typedef struct pib_t_ {
    uint16    mac_ack_wait_duration;
    bool     mac_associated_pan_coord;
    bool     mac_association_permit;
    bool     mac_auto_request;
    uint8    mac_beacon_order;
//    uint8    mac_bsn;
    uint64   mac_coord_extended_addr;
    uint16   mac_coord_short_addr;
    uint8    mac_dsn;
    bool     mac_gts_permit;
    uint8    mac_max_be;
    uint8    mac_max_csma_backoffs;
    uint16   mac_max_frame_total_wait_time;
    uint8    mac_max_frame_retries;
    uint8    mac_min_be;
    uint16   mac_pan_id;
    bool     mac_promiscuous_mode;
    uint16    mac_response_wait_time;
    bool     mac_rx_on_when_idle;
    bool     mac_security_enabled;
    uint16   mac_short_addr;
    uint8    mac_superframe_order;
    uint16   mac_transaction_persistence_time;
    uint16   mac_sup_frm_value;
} pib_t;

/*! the MAC state type */
typedef enum mac_state_t_ {
    mac_idle = 0,
    mac_scanning = 1, /*!< set when channel scan except ed scan is requested and started. */
    mac_ed_scanning = 2, /*!< set when ed scan is requested and started. */
    mac_assoc_reqing = 3, /*!< set when association request is started */
    mac_poll_reqing = 4, /*!< set when poll request is started */
    mac_assoc_poll_reqing = 5, /*!< set when poll request is started, and mac_assoc_reqing is on */
    /* the mac_assoc_reqing and mac_poll_reqing state should be seperatelly represented, 
       cause it may be set at the same time when association, so cool! */
} mac_state_t;

/*! MAC control block type */
typedef struct mac_cb_t_ {
    mac_state_t state;
    mtimer_t mac_timer;
    uint8 dsn;
} mac_cb_t;

/*! MAC event type */
typedef enum mac_event_t_ {
    mac_event_frm_recv,
} mac_event_t;

int8 mac_info_init(void);
int8 mac_pib_init(void);
int8 mac_mcb_init(void);

uint16 mac_get_pan_id(void);
int8 mac_set_pan_id(uint16 pan_id);
int8 mac_set_state(mac_state_t stat);
int8 mac_get_state(void);
int8 mac_recv_handler(void);

mtimer_t *mac_mcb_get_timer(void);

int8 mac_reset(void);

uint64 mac_get_extended_addr(void);
int8 mac_set_extended_addr(uint64 addr);

uint8 mac_get_dsn(void);
int8 mac_set_dsn(uint8 dsn);

int8 mac_set_coord_short_addr(uint16 addr);
int8 mac_set_coord_extended_addr(uint64 addr);

uint64 make_extended_addr(uint8 *arr);

uint16 mac_get_coord_short_addr(void);
uint64 mac_get_coord_extended_addr(void);

uint16 mac_get_short_addr(void);
int8 mac_set_short_addr(uint16 addr);

uint16 mac_get_sup_frm_value(void);
uint16 mac_set_sup_frm_value(uint16 v);

uint16 mac_get_response_wait_time(void);
uint16 mac_set_response_wait_time(uint16 t);

uint16 mac_get_max_frame_total_wait_time(void);
uint16 mac_set_max_frame_total_wait_time(uint16 t);

uint16 mac_set_transaction_persistence_time(uint16 t);
uint16 mac_get_transaction_persistence_time(void);

uint8 mac_get_max_csma_backoffs(void);
uint8 mac_set_max_csma_backoffs(uint8 n);

uint8 mac_set_max_be(uint8 b);
uint8 mac_get_max_be(void);

uint8 mac_set_min_be(uint8 b);
uint8 mac_get_min_be(void);

uint16 mac_set_ack_wait_duration(uint16 t);
uint16 mac_get_ack_wait_duration(void);

uint8 mac_set_max_frame_retries(uint8 r);
uint8 mac_get_max_frame_retries(void);

uint8 mac_get_max_frame_retries(void);
uint8 mac_set_max_frame_retries(uint8 r);

uint8 mac_set_bcn_order(uint8 o);
uint8 mac_get_bcn_order(void);

uint8 mac_set_superframe_order(uint8 s);
uint8 mac_get_superframe_order(void);

bool mac_set_associated_pan_coord(bool b);
bool mac_get_associated_pan_coord(void);

uint8 mac_set_beacon_order(uint8 b);
uint8 mac_get_beacon_order(void);

bool mac_set_association_permit(bool b);
bool mac_get_association_premit(void);

uint16 mac_set_sup_frm_value(uint16 v);
uint16 mac_get_sup_frm_value(void);


#endif /* _MAC_CTRL_H_ */
